﻿Public Partial Class wfrmValidaCNS
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        If ValidaCNS(Me.TextBox1.Text) Then
            Me.lblOK.Text = "Opa!! esse código é válido hein!"
        Else
            Me.lblOK.Text = "Código inválido!!"
        End If
        Me.lblOK.Visible = True
    End Sub

    Function ValidaCNS(ByVal Numero As String) As Boolean
        Dim resto As Integer = 0
        Dim soma As Long = 0
        soma = _
        ((CLng(Mid(Numero, 1, 1))) * 15) + _
        ((CLng(Mid(Numero, 2, 1))) * 14) + _
        ((CLng(Mid(Numero, 3, 1))) * 13) + _
        ((CLng(Mid(Numero, 4, 1))) * 12) + _
        ((CLng(Mid(Numero, 5, 1))) * 11) + _
        ((CLng(Mid(Numero, 6, 1))) * 10) + _
        ((CLng(Mid(Numero, 7, 1))) * 9) + _
        ((CLng(Mid(Numero, 8, 1))) * 8) + _
        ((CLng(Mid(Numero, 9, 1))) * 7) + _
        ((CLng(Mid(Numero, 10, 1))) * 6) + _
        ((CLng(Mid(Numero, 11, 1))) * 5) + _
        ((CLng(Mid(Numero, 12, 1))) * 4) + _
        ((CLng(Mid(Numero, 13, 1))) * 3) + _
        ((CLng(Mid(Numero, 14, 1))) * 2) + _
        ((CLng(Mid(Numero, 15, 1))) * 1)

        resto = soma Mod 11
        If resto <> 0 Then
            Return False
        Else
            Return True
        End If
    End Function

End Class